package com.rest.demo.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rest.demo.Service.Dysmtl;
import com.rest.demo.mapper.UsersMapper;
import com.rest.demo.repository.DysmtUser;
import com.rest.demo.repository.UserHealthInfo;
import com.rest.demo.util.Sm4Util;
import com.rest.demo.util.TimeDate;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Data
@RestController
@RequestMapping("/DysmtHealth")//整个路由路径

public class GetUserHeathInfo {
    @Resource
    public UsersMapper usersMapper;

    public String signature = "b1bc547c38e58853706d0ba56e8cd13";
    public DysmtUser RcUser = new DysmtUser();


//
//
//    @RequestMapping("/test")
//    public String test() {
//
//
//
//        RcUser.setCarId("CarId");
//        RcUser.setUserName("UserName");
//        RcUser.setNickname("1");
//        RcUser.setPhone("17313368128");
//        RcUser.setRegister((byte)0);
//
//        boolean flag = usersMapper.add(RcUser) > 0;
//        System.out.println(flag);
//
//
//
//        return "hello";
//    }


    //完整健康信息 不带token
    @RequestMapping("/GetHealthInfo")
    public JSONObject TGetUserHeathInfo(String UserName, String CarId) {

        this.SaveAllData(UserName,CarId);//记录
        Dysmtl DyHealth = new Dysmtl();
        JSONObject ReportInfo = DyHealth.GetHealthInfo(UserName, CarId);


//System.out.println(RcUser);
//        System.out.println(ReportInfo.getJSONObject("data").getJSONObject("person").getString("desensitizeCardId"));
        return ReportInfo;
    }

    //返回状态码 带token
    @RequestMapping("/GetHealthInfoRepoet")
    public JSONArray GetUserHeathInfoRepoet(String UserName, String CarId, String Token) {
        JSONArray states = new JSONArray();
        String decDatacontent = Sm4Util.dataDecrypt(Token, signature);//解密///
//        System.out.println(decDatacontent.equals("德阳大数据局"));
        if (decDatacontent.equals("德阳大数据局") || decDatacontent.equals("医院") || decDatacontent.equals("硬件")) {
            Dysmtl DyHealth = new Dysmtl();
            JSONObject ReportInfo = DyHealth.GetHealthInfo(UserName, CarId);

            JSONArray Report = ReportInfo.getJSONObject("data").getJSONArray("healthyReport");

//            int rep = 0;
            // 使用传统for循环进行遍历
            for (int i = 0, size = Report.size(); i < size; i++) {
                String value = Report.getJSONObject(i).getString("healthyStaus");
//                rep = rep + Integer.valueOf(value);

                if (value.equals("00")) {
                    states.add("绿码");
                } else if (value.equals("01")) {
                    states.add("黄码");
                } else {
                    states.add("红码");
                }
            }
            this.SaveAllData(UserName,CarId);//记录

//        System.out.println(ReportInfo.getJSONObject("data").getJSONObject("person").getString("desensitizeCardId"));
            return states;
        } else {
            JSONArray states2 = new JSONArray();
            states2.add("错误的密钥获取非法用户");
            return states2;

        }

    }






    //核酸检测 带token
    @RequestMapping("/GetHealthInfoDNA")
    public JSONObject GetUserHeathInfoRepoetTemp(String UserName, String CarId, String Token) {
        String decDatacontent = Sm4Util.dataDecrypt(Token, signature);//解密///
//        System.out.println(decDatacontent.equals("德阳大数据局"));
        if (decDatacontent.equals("德阳大数据局") || decDatacontent.equals("医院") || decDatacontent.equals("硬件")) {
            Dysmtl DyHealth = new Dysmtl();
            JSONObject ReportInfo = DyHealth.GetHealthInfo(UserName, CarId);
            JSONObject Report = ReportInfo.getJSONObject("data").getJSONObject("hsjc");
            this.SaveAllData(UserName,CarId);//记录
            return Report;
        }else {


            return null;

        }

    }

    //生码
    @RequestMapping("/Md5JackT")
    public String MD5Code(String Code) {

//        String decData = Sm4Util.dataEncrypt(Code, signature);//加密【请求参数加密】
        return Sm4Util.dataEncrypt(Code, signature);
    }

    public void SaveAllData(String UserName,String CarId) {

        TimeDate hh= new TimeDate();
        RcUser.setUserName(UserName);
        RcUser.setCarId(CarId);
        RcUser.setPhone("不提供");
        RcUser.setNickname( hh.NowTime());
        RcUser.setRegister((byte)1);
        boolean flag = usersMapper.add(RcUser) > 0;
        System.out.println(flag);

    }


    @RequestMapping("/GetHealthInfo2")
    public UserHealthInfo GetUserHeathInfo2(String UserName, String CarId) {
        Dysmtl DyHealth = new Dysmtl();
        UserHealthInfo userinfo = new UserHealthInfo();
        JSONObject ReportInfo = DyHealth.GetHealthInfo(UserName, CarId);
        userinfo.setPerson(ReportInfo.getJSONObject("data").getJSONObject("person"));
        userinfo.setHsjc(ReportInfo.getJSONObject("data").getJSONObject("hsjc")); // 核酸检测结果
        userinfo.setKtjc(ReportInfo.getJSONObject("data").getJSONObject("ktjc")); // 抗体检测结果
        userinfo.setYqDatas(ReportInfo.getJSONObject("data").getJSONArray("yqDatas"));
        userinfo.setHasReport(ReportInfo.getJSONObject("data").getBoolean("hasReport"));//是否有健康报告
        userinfo.setYqWarn(ReportInfo.getJSONObject("data").getJSONObject("yqWarn"));//警告
        userinfo.setHealthyReport(ReportInfo.getJSONObject("data").getJSONArray("healthyReport"));//健康状态警告
        userinfo.setRjcx(ReportInfo.getJSONObject("data").getJSONArray("rjcx"));//
        userinfo.setXcgj(ReportInfo.getJSONObject("data").getJSONArray("xcgj"));//

        return userinfo;
    }


}
